package com.xxl.job.admin.repository;

import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
import com.xxl.job.admin.core.enums.DatabaseTypeEnum;
import com.xxl.job.admin.core.model.XxlJobLogGlue;
import com.xxl.job.admin.dao.XxlJobLogGlueDao;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class XxlJobLogGlueDaoRepository implements XxlJobLogGlueDao {

    private final XxlJobLogGlueDao xxlJobLogGlueDao;

    public XxlJobLogGlueDaoRepository(XxlJobLogGlueDao xxlJobLogGlueDao) {
        this.xxlJobLogGlueDao = xxlJobLogGlueDao;
    }

    @Override
    public int save(XxlJobLogGlue xxlJobLogGlue) {
        DatabaseTypeEnum typeEnum = DatabaseTypeEnum.getByType(XxlJobAdminConfig.getAdminConfig().getDatabaseId());
        switch (typeEnum) {
            case MYSQL:
                return xxlJobLogGlueDao.save(xxlJobLogGlue);
            case ORACLE:
                // 从Oracle序列中获取ID并设值
                xxlJobLogGlue.setId(xxlJobLogGlueDao.getNextOracleSequenceId());
                return xxlJobLogGlueDao.saveWithId(xxlJobLogGlue);
            default:
                throw new RuntimeException("un support database type");
        }
    }

    @Override
    public int getNextOracleSequenceId() {
        return xxlJobLogGlueDao.getNextOracleSequenceId();
    }

    @Override
    public int saveWithId(XxlJobLogGlue xxlJobLogGlue) {
        return xxlJobLogGlueDao.saveWithId(xxlJobLogGlue);
    }

    @Override
    public List<XxlJobLogGlue> findByJobId(int jobId) {
        return xxlJobLogGlueDao.findByJobId(jobId);
    }

    @Override
    public int removeOld(int jobId, int limit) {
        return xxlJobLogGlueDao.removeOld(jobId, limit);
    }

    @Override
    public int deleteByJobId(int jobId) {
        return xxlJobLogGlueDao.deleteByJobId(jobId);
    }
}
